/*
 *   Copyright 2008 Francois Fernandes
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.googlecode.opendoclibrary.internal.impl.fs.meta;

import java.util.Iterator;

import com.googlecode.opendoclibrary.library.spi.IMetadataEntry;
import com.googlecode.opendoclibrary.library.spi.IMetadataView;
import com.googlecode.opendoclibrary.library.spi.RegisteredNamespace;

class View implements IMetadataView
{
    private final Metadata target;

    private final RegisteredNamespace viewNamespace;

    public View( RegisteredNamespace viewNamespace, Metadata target )
    {
        super();
        this.target = target;
        if ( viewNamespace == null )
        {
            throw new IllegalArgumentException( "viewNamespace may not be null" );
        }
        this.viewNamespace = viewNamespace;
    }

    @Override
    public String getString( String key )
    {
        return target.getString( viewNamespace.getNamespacePrefix(), key );
    }

    @Override
    public RegisteredNamespace getViewNamespaces()
    {
        return viewNamespace;
    }

    @Override
    public Iterator<IMetadataEntry> iterator()
    {
        return new ViewIterator( viewNamespace.getNamespacePrefix(),
                                 target.getNsLocal( viewNamespace.getNamespacePrefix() ).entrySet().iterator() );
    }

    @Override
    public void putString( String key, String value )
    {
        target.putString( viewNamespace.getNamespacePrefix(), key, value );
    }
}